<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FourRest;
use App\Models\Fourbusiness;
use App\Models\FourInpatient;
use App\Models\FourPatient;
use App\Models\FourPay;
use App\Models\FourSource;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FourRestController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FourRest(['es','source','type','pay','des']), function (Grid $grid) {
          //  $grid->column('id')->sortable();
            $grid->column('created_at');
            $grid->column('order_id','订单号');
            $grid->column('es.patient_name','患者姓名');
            $grid->column('visit_id','就诊卡号');
            $grid->column('des.inpatient_name','住院号');
            $grid->column('source.source_name','来源');
            $grid->column('type.bussiness_name','业务类型');
            $grid->column('pay.pay_name','支付方式');
            $grid->column('money');
            $grid->column('status')->using(['1'=>'<font color="green">已住院</font>','2'=>'<font color="red">未出院</font>']);
            $grid->column('balance');
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableRefreshButton();
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->export()->titles('住院充值记录')->filename('住院充值记录')->rows(function ($rows){
                foreach ($rows as $index => &$row){
                    if ($row['status']==1){
                        $row['status']='已支付';
                    }elseif ($row['status']==2){
                        $row['status']='未支付';
                    }
                }
                return $rows;
            });
           // $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->between('created_at', '选择日期')->datetime()->width(200);
                $filter->equal('es.patient_name','患者姓名')->width(3);
                $filter->equal('visit_id','就诊卡号')->width(3);
            });

           /* $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });*/
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        Admin::css('/css/card.css');
        $data = \App\Models\FourRest::with(['es','source','type','pay','des'])->find($id);
         return admin_view('rest',compact('data'));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FourRest(), function (Form $form) {
         //   $form->display('id');
            $form->display('created_at');
            $form->select('order_id');
            $form->select('patient_id')->options(FourPatient::pluck('patient_name','id')->toArray());
            $form->select('visit_id');
            $form->select('inpatient_id')->options(FourInpatient::pluck('inpatient_name','id')->toArray());
            $form->select('source_id')->options(FourSource::pluck('source_name','id')->toArray());
            $form->select('bussiness_type')->options(Fourbusiness::pluck('bussiness_name','id')->toArray());
            $form->select('pay_id')->options(FourPay::pluck('pay_name','id')->toArray());
            $form->text('money');
            $form->radio('status')->options(['1'=>'已住院','2'=>'未出院'])->default(0);
            $form->text('balance');
           // $form->display('updated_at');
        });
    }
}
